package com.lili.math;

/**
 * @Auther: 李 力
 * @Date: 2024/8/23
 * @Description: com.lili.math
 * @version: 1.0
 */
public class LeetCode50 {
    public static double myPow(double x, int n) {
        int p = n;
        if (p < 0) {
            p = -p;
        }
        double v = quickPow(x, p);
        return n < 0 ? 1 / v : v;
    }

    private static double quickPow(double x, int n) {
        if (n == 0) {
            return 1.0;
        }
        if (x == 1.0) {
            return x;
        }
        double r = quickPow(x, n / 2);
        if (n % 2 == 0) {
            return r * r;
        } else {
            return r * r * x;
        }
    }
}
